<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="listQuery" size="small" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="listQuery.userName" placeholder="请输入用户名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button size="small" type="primary" @click="getDataList()">搜索</el-button>
        </el-form-item>
        <el-form-item>
          <el-button size="small" @click="handleResetSearch()">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="table-container">
      <el-table :data="list" border v-loading="dataListLoading" @selection-change="selectionChangeHandle"
        style="width: 100%;">
        <el-table-column prop="userName" label="预约用户" header-align="center" align="center"></el-table-column>
        <el-table-column prop="serviceName" label="预约服务" header-align="center" align="center"></el-table-column>
        <el-table-column prop="reservationDate" label="预约日期" header-align="center" align="center"></el-table-column>
        <el-table-column prop="reservationTime" label="预约时间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="status" label="预约状态" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0" size="small">预约中</el-tag>
            <el-tag v-if="scope.row.status === 1" size="small" type="success">已完成</el-tag>
            <el-tag v-if="scope.row.status === 2" size="small" type="danger">已取消</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="note" label="客户留言" header-align="center" align="center"></el-table-column>
        <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="addOrUpdateHandle(scope.row.id)">预约详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页控件 -->
    <el-pagination background class="table-pagination" @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle" :current-page="listQuery.pageIndex" :page-size="listQuery.pageSize"
      :total="total" :page-sizes="[10, 20, 50, 100]" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import { fetchReservationList, deleteReservation } from '@/api/reservation/reservation';
import AddOrUpdate from './reservation-add-or-update';
const baseUrl = process.env.VUE_APP_FILE_URL === '/' ? '' : process.env.VUE_APP_FILE_URL;

const defaultListQuery = {
  pageIndex: 1,
  pageSize: 10,
};

export default {
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      total: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      baseApi: baseUrl,
    }
  },
  components: {
    AddOrUpdate
  },
  created() {
    this.getDataList()
  },
  methods: {
    // 重置查询表单
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      fetchReservationList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.listQuery.pageSize = val
      this.listQuery.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.listQuery.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },

  }
}
</script>
